package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Bom;
import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.PartDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.PartEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;

@Component
public class PartDataHandler extends PartRelateDataHandler<Part>{
	
	@Autowired
	private PartDao partDao;
	@Autowired
	private BomDataHandler bomHandler;
	

	
	@Override
	public Part getById(long id) {
		Part part = new Part();
		part.setInnerData(partDao.getById(id));
		return part;
	}

	public Part getByName(String name) {
		Part part = new Part();
		part.setInnerData(partDao.getByName(name));
		return part;
	}
	
	public Part getPartByPartNo(String partNo,String revision) {
		Part part = new Part();
		PartEntity partByPartNo = partDao.getPartByPartNo(partNo, revision);
		if(partByPartNo!=null) {
			part.setInnerData(partByPartNo);
			String bomName = partByPartNo.getBomName();
			String bomRevision = partByPartNo.getBomRevision();
			if(bomName!=null && !"".equals(bomName) && bomRevision!=null && !"".equals(bomRevision)) {
				Bom bom = bomHandler.getByNameAndRevision(bomName, bomRevision);
				part.setBom(bom);
			}
		}
		return part;
	}
	
	public Part getPartByPartNo(String partNo) {
		Part part = new Part();
		part.setInnerData(partDao.getByName(partNo));
		return part;
		
	}

	public List<Part> getAllPart() {
		List<PartEntity> entityList = partDao.getAllPart();
		List<Part> partList = new ArrayList<Part>();
		if (entityList != null && entityList.size() > 0) {
			for(PartEntity entityData:entityList) {
				Part part = new Part();
				part.setInnerData(entityData);
				partList.add(part);
			}
			return partList;
		}
		return null;
	}

	public List<Part> getPartByBomNameAndRevision(String bomName, String bomRevision) {
		List<PartEntity> entityList = partDao.getPartByBomNameAndRevision(bomName, bomRevision);
		List<Part> partList = new ArrayList<Part>();
		if (entityList != null && entityList.size() > 0) {
			for(PartEntity entityData:entityList) {
				Part part = new Part();
				part.setInnerData(entityData);
				partList.add(part);
			}
			return partList;
		}
		return null;
	}

	public List<PartEntity> getPartByPartNoAndRevision(Part part) {
		List<PartEntity> entityList = partDao.getPartByPartNoAndRevision(part.getInnerData().getPartNo(), part.getInnerData().getRevision(),part.getStart(),part.getLength());
		if (entityList != null && entityList.size() > 0) {
			return entityList;
		}
		return null;
	}


	@Override
	public ResultBean saveBatch(List<Part> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Part> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Part> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean save(Part obj) {
		ResultBean resultBean = new ResultBean(true);
		BaseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int save = partDao.save(obj.getInnerData());
		if (save > 0) {
			return resultBean;
		}
		return null;
	}

	@Override
	public ResultBean update(Part obj) {
		ResultBean resultBean = new ResultBean(true);
		BaseEntityDataUtil.updateBaseEntityData(obj.getInnerData());
		int update = partDao.update(obj.getInnerData());
		if (update > 0 ) {
			return resultBean;
		}
		return null;
	}

	@Override
	public ResultBean delete(Part obj) {
		ResultBean resultBean = new ResultBean(true);
		partDao.delete(obj.getInnerData());
		return resultBean;
	}
	
	public ResultBean validatePartDelete(Part part) {
		ResultBean resultBean = new ResultBean(true);
		// 校验物料在采购单中是否被引用
		 int countByPartId = partDao.getCountByPartId(part.getInnerData().getId(), "wms_purchase_order");
		 if (countByPartId>0) {
			 resultBean.setErrorMessage("当前物料被采购单数据引用,无法删除！");
			 return resultBean;
		 }
		 // 校验物料在入库单中是否被引用
		  int countByPartId2 = partDao.getCountByPartId(part.getInnerData().getId(), "wms_in_warehouse_order");
		 if (countByPartId2>0) {
			 resultBean.setErrorMessage("当前物料被入库单数据引用，无法删除！");
			 return resultBean;
		 }
		 // 校验物料在出库单中是否被引用
		 int countByPartId3 = partDao.getCountByPartId(part.getInnerData().getId(), "wms_out_warehouse_order");
		 if (countByPartId3>0) {
			 resultBean.setErrorMessage("当前物料被出库单数据引用，无法删除！");
			 return resultBean;
		 }
		 
		// 校验物料在调拨单中是否被引用
		 int countByPartId4 = partDao.getCountByPartId(part.getInnerData().getId(), "wms_allocation_order");
		 if (countByPartId4>0) {
			 resultBean.setErrorMessage("当前物料被调拨单数据引用，无法删除！");
			 return resultBean;
		 }
		 
		// 校验物料在库存中是否被引用
		int countByPartId5 = partDao.getCountByPartId(part.getInnerData().getId(), "wms_storage");
		if (countByPartId5>0) {
			 resultBean.setErrorMessage("当前物料被库存数据引用，无法删除！");
			 return resultBean;
		 }
		int countByPartId6 = partDao.getCountByPartId(part.getInnerData().getId(), "mes_pp_plan");
		if (countByPartId6 > 0) {
			resultBean.setErrorMessage("当前物料被计划管理数据引用，无法删除！");
			 return resultBean;
		}
		 return resultBean;
	}

}
